﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataBaseTable;
using MySpace.Data;

public partial class Manage_DengXin_Book_Mode : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!string.IsNullOrEmpty(Request.QueryString["bookid"]))
        {
            this.BookId.Value = Request.QueryString["bookid"];
            submit.Text = "更新文集";
            submit.Click += new EventHandler(UpdatePeriods_Click);
            UpdatePanel2.Visible = true;
            Chapter_lb.Visible = true;
            if (!IsPostBack)
            {
                this.picguid.Value = Guid.NewGuid().ToString();
                this.DropDownListbd();
                this.initData();
            }
        }
        else
        {
            if (!IsPostBack)
            {
                this.picguid.Value = Guid.NewGuid().ToString();
                this.DropDownListbd();
            }
            submit.Text = "添加并创建章节";
            submit.Click += new EventHandler(AddPeriods_Click); 
        }
    }
    private void initData()
    {
        DengXinBookTable dxbt = new DengXinBookTable(Convert.ToInt32(this.BookId.Value));
        if (dxbt.BookId == 0)
        {
            //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('ID非法');</script>");
            Response.Redirect("~/Manage/DengXin_Book.aspx");
        }
        PeriodsName.Text = dxbt.PeriodsName;
        DIndex.Text = dxbt.DIndex.ToString();
        PublishYear.SelectedValue = dxbt.PublishYear;
        this.filename.Value = dxbt.Cover;
        pic.ImageUrl = "~/DengXinCover/" + this.filename.Value;
        this.CatalogueBind();
    }
    private void CatalogueBind()
    {
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = string.Format("select * from DengXinCatalogue where ForPeriodsId={0}",this.BookId.Value);
        Chapter_gv.DataSource = dbh.ExecuteDataTable();
        Chapter_gv.DataBind();
    }
    private void DropDownListbd()
    {
                //绑定年代
        PublishYear.Items.Add("出版年份");
        for (int i = Convert.ToInt32(DateTime.Now.Year); i >= 1997; i--)
        {
            PublishYear.Items.Add(i.ToString() + "年");
        }
    }
    protected void UpLoadPic_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(PicFileUpload.PostedFile.FileName))
        {
            string filepath = PicFileUpload.PostedFile.FileName;
            string fileextname = filepath.Substring(filepath.LastIndexOf(".") + 1);//扩展名
            if (!(fileextname.ToLower() == "jpg" || fileextname.ToLower() == "jpeg" || fileextname.ToLower() == "gif" || fileextname.ToLower() == "png" || fileextname.ToLower() == "bmp"))
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('选择的文件不是图片格式');</script>");
                return;
            }
            string mpath = Server.MapPath("~/DengXinCover/");//文件夹路径
            this.filename.Value=this.picguid.Value+"."+fileextname;
            string mfilename = this.picguid.Value;
            try
            {
                PicFileUpload.PostedFile.SaveAs(mpath + mfilename + "." + fileextname);
                this.pic.ImageUrl = "~/DengXinCover/" + mfilename + "." + fileextname;
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加图片成功');</script>");
                //DataBd();
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加图片失败');</script>");
            }
        }
    }
    protected void AddPeriods_Click(object sender, EventArgs e)
    {
        try
        {
            DengXinBookTable dxbt = new DengXinBookTable();
            dxbt.PeriodsName = PeriodsName.Text;
            dxbt.DIndex = Convert.ToInt32(DIndex.Text);
            dxbt.PublishYear = PublishYear.Text;
            dxbt.Cover = this.filename.Value;
            dxbt.Insert();
            DBHandler dbh = GetHandler.Init();
            dbh.CommandText = "select top 1 BookId from DengXinBook order by BookId desc";
            this.BookId.Value= dbh.ExecuteScalar().ToString();
            Response.Redirect("~/Manage/DengXin_Book_Mode.aspx?bookid=" + this.BookId.Value);

            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('创建文集成功,请继续创建章节');</script>");
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('创建文集失败');</script>");
        }
    }
    protected void UpdatePeriods_Click(object sender, EventArgs e)
    {
        DengXinBookTable dxbt = new DengXinBookTable(Convert.ToInt32(this.BookId.Value));
        if (dxbt.BookId == 0)
        {
            return;
        }
        dxbt.PeriodsName = PeriodsName.Text;
        dxbt.DIndex = Convert.ToInt32(DIndex.Text);
        dxbt.PublishYear = PublishYear.Text;
        dxbt.Cover = this.filename.Value;
        dxbt.Update();
        ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新文集成功');</script>");
    }
    protected void Chapter_gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Chapter_gv.EditIndex = e.NewEditIndex;
        this.CatalogueBind();
    }
    protected void Chapter_gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        Chapter_gv.EditIndex = -1;
        this.CatalogueBind();
    }
    protected void Chapter_gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            string id = ((Label)Chapter_gv.Rows[e.RowIndex].FindControl("id")).Text;
            DengXinCatalogueTable dxct = new DengXinCatalogueTable(Convert.ToInt32(id));
            dxct.ChapterName = ((TextBox)Chapter_gv.Rows[e.RowIndex].FindControl("ChapterName")).Text;
            dxct.Update();
            ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('更新章节成功')", true);
        }
        catch
        {
            ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('更新章节失败')", true);
        }
        Chapter_gv.EditIndex = -1;
        this.CatalogueBind();
    }
    protected void Chapter_gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string id = ((Label)Chapter_gv.Rows[e.RowIndex].FindControl("id")).Text;
            DengXinCatalogueTable dxct = new DengXinCatalogueTable(Convert.ToInt32(id));
            dxct.Delete();
            ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('删除章节成功')", true);
        }
        catch
        {
            ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('更新章节失败')", true);
        }
        this.CatalogueBind();

    }
    protected void add_chapter_btn_Click(object sender, EventArgs e)
    {
        DengXinCatalogueTable dxct = new DengXinCatalogueTable();
        dxct.ForPeriodsId =Convert.ToInt32(this.BookId.Value);
        dxct.ChapterName = ChapterName_tb.Text;
        dxct.Insert();
        ChapterName_tb.Text = "";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel2, this.GetType(), "click", "alert('添加章节成功')", true);
        this.CatalogueBind();
    }
}